package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketFeedback;
import com.cskaoyan.market.service.MarketFeedBackService;
import com.cskaoyan.market.service.MarketFeedBackServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: YaLiFeng
 * @description: TODO
 * @date: 2024/5/8 14:20
 */
@WebServlet("/admin/feedback/*")
public class AdminFeedBackController extends HttpServlet {
    private MarketFeedBackService marketFeedBackService = new MarketFeedBackServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/feedback/", "");
        if ("list".equals(op)) {
            list(resp, req);
        }
    }

    private void list(HttpServletResponse resp, HttpServletRequest req) throws IOException {
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String usernameParam = req.getParameter("username");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String id = req.getParameter("id");
        Integer page = null;
        Integer limit = null;
        ServletContext servletContext = getServletContext();
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
        } catch (NumberFormatException e) {
            Object object = ResponseUtil.badArgument();
            String s = JacksonUtil.getObjectMapper().writeValueAsString(object);
            return;
        }
        List<MarketFeedback> feedbackList = marketFeedBackService.feedBackList(page, limit, usernameParam, sort, order, id);

        String relativePath = (String) servletContext.getAttribute("path");
        for (MarketFeedback feedback : feedbackList) {
            List<String> picUrls = new ArrayList<>();
            if (feedback.getHasPicture()) {
                for (int i = 0; i < feedback.getPicUrls().length; i++) {
                    String url = feedback.getPicUrls()[i];
                    String domain = (String) servletContext.getAttribute("domain1");
                    String picUrl = domain + url;
                    picUrls.add(picUrl);
                }
                feedback.setPicUrls(picUrls.toArray(new String[]{}));
            }
        }


        Object o = ResponseUtil.okList(feedbackList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));


    }


}
